//setup
   clear all
   set matsize 4000
   args SEED REPS TYPE
   capture cd "D:/DATA.IDB/Dropbox/1.Research/2.Methods/BDM/BDM2012/code/"
     if _rc==0 global cd = "D:/DATA.IDB/Dropbox/1.Research/2.Methods/BDM/BDM2012/code/"
   capture cd "D:/Dropbox/My Dropbox/1.Research/2.Methods/BDM/BDM2012/code/"
     if _rc==0 global cd = "D:/Dropbox/My Dropbox/1.Research/2.Methods/BDM/BDM2012/code/"
   capture cd "/accounts/fac/jmccrary/research/BDM2012/code/"
     if _rc==0 global cd = "/accounts/fac/jmccrary/research/BDM2012/code/"
   global dir = "$cd/"

   local estimators = "nn1_X nn2_X nn3_X nn4_X bcm1_X bcm2_X bcm3_X bcm4_X nn1_pX nn2_pX nn3_pX nn4_pX bcm1_pX bcm2_pX bcm3_pX bcm4_pX llr ipw1 ipw2 gpe"
   local order      = "nn1_X nn2_X nn3_X nn4_X bcm1_X bcm2_X bcm3_X bcm4_X __S1__ nn1_pX nn2_pX nn3_pX nn4_pX bcm1_pX bcm2_pX bcm3_pX bcm4_pX llr __S2__ ipw1 ipw2 gpe"

//A1: Frolich DGP (Bias). Full results.
  local i = 1
  foreach type in frolich frolich_est frolich001 frolich_est001{
    use "$dir/SIMS`type'/sim`type'.dta", replace
    collapse (mean) `estimators' truth, by(design curve)
	foreach var in `estimators'{
	  replace `var'= (`var'-truth)*1000
	}
    drop truth
    gen __S1__ = .
    gen __S2__ = .
    order design curve `order'
	if `i'==1 local rep = "replace"
	if `i'>1  local rep = "sheetreplace"
	** put the name of the dgp in the last row of the table
	tostring design, replace
	count
	local N = r(N)+1
	set obs `N'
	replace design = "`type'" in `N'
    export excel "$dir/tables.xlsx", `rep' sheet("A1-`i'") firstrow(variables)
    save "$dir/A1`i'.dta", replace
	local i = `i'+1
}	
//A1: Frolich DGP (Var). Full results.
  foreach type in frolich frolich_est frolich001 frolich_est001{
    use "$dir/SIMS`type'/sim`type'.dta", replace
	foreach var in `estimators'{
	  egen mean = mean(`var'), by(design curve)
	  replace `var'= 100*(`var'-mean)^2
	  drop mean
    }
    collapse (mean) `estimators', by(design curve)
	gen __S1__ = .
	gen __S2__ = .
	order design curve `order'
	** put the name of the dgp in the last row of the table
	tostring design, replace
    count
	local N = r(N)+1
	set obs `N'
	replace design = "`type'" in `N'
	export excel "$dir/tables.xlsx", sheetreplace sheet("A1-`i'") firstrow(variables)
    save "$dir/A1`i'.dta", replace
	local i = `i'+1
}

//A2: NSW. Full results.
    use "$dir/SIMSnsw/simnsw.dta", replace
	// Bias column
    collapse (mean) `estimators' truth, by(overlap)
    foreach var in `estimators'{
 	 replace `var'= (`var'-truth)*1000
    }
    drop truth
	order overlap `estimators'
	xpose, varname clear
	order _varname
	ren v1 bias1
	ren v2 bias5
	tempfile nswbias
	save `nswbias', replace
    // Variance column
    use "$dir/SIMSnsw/simnsw.dta", replace
	foreach var in `estimators'{
	 egen mean = mean(`var'), by(overlap)
	 replace `var'= 780*(`var'-mean)^2
	 drop mean
	}
	collapse (mean) `estimators', by(overlap)
	order overlap `estimators'
	xpose, varname clear
	order _varname
	ren v1 variance1
	ren v2 variance5
	// Organize table
	merge 1:1 _varname using `nswbias'
	drop _merge
	order _varname bias1 variance1 bias5 variance5 
	gen order = 1 if _varname=="overlap"
    local j = 2
	foreach var in `estimators'{
	 replace order = `j' if _varname=="`var'"
	 local j = `j'+1
	}
	sort order
	drop order
	ren _varname col1
	gen __S1__ = .
	gen __S2__ = .
    order col1 __S1__ bias1 variance1 __S2__ bias5 variance5 
    export excel "$dir/tables.xlsx", sheetreplace sheet("A2") firstrow(variables)
    save "$dir/A2.dta", replace

// A3a: Bias in Misspecified models
    use "$dir/SIMSmisspec/simmisspec.dta", replace
	collapse (mean) `estimators' truth, by(wiggle pscore_model biasadj_model)
	foreach var in `estimators'{
	  replace `var'= (`var'-truth)*1000
	 }
	drop truth
	gen __S1__ = .
	gen __S2__ = .
	ren wiggle dgp
	replace dgp = round(dgp*10,1)
	label define dgp 0 "Linear" 10 "Interactions" 5 "Linear+Interactions" 100 "Lin-Int" 200 "Int-Lin"
	label values dgp dgp
	order dgp pscore_model biasadj_model `order'
	export excel "$dir/tables.xlsx", sheetreplace sheet("A3a") firstrow(variables)
    save "$dir/A3a.dta", replace

    use "$dir/SIMSmisspec/simmisspec.dta", clear
	foreach var in `estimators'{
	  egen mean = mean(`var'), by(wiggle pscore_model biasadj_model)
	  replace `var'= 400*(`var'-mean)^2
	  drop mean
	 }
	collapse (mean) `estimators', by(wiggle pscore_model biasadj_model)
	gen __S1__ = .
	gen __S2__ = .
	ren wiggle dgp
	replace dgp = round(dgp*10,1)
	label define dgp 0 "Linear" 10 "Interactions" 5 "Linear+Interactions" 100 "Lin-Int" 200 "Int-Lin"
	label values dgp dgp
	order dgp pscore_model biasadj_model `order'
    export excel "$dir/tables.xlsx", sheetreplace sheet("A3b") firstrow(variables)
    save "$dir/A3b.dta", replace

// Main Tables: 
    local estimators = "nn1_X nn4_X bcm1_X bcm4_X nn1_pX nn4_pX bcm1_pX bcm4_pX llr ipw1 ipw2 gpe"
    local order      = "nn1_X nn4_X bcm1_X bcm4_X __S1__ nn1_pX nn4_pX bcm1_pX bcm4_pX llr __S2__ ipw1 ipw2 gpe"

// Table 1
forvalues k = 1(1)8{	
    use "$dir/A1`k'.dta", replace
	drop if _n>30
	keep design curve `order'
	
	preserve
	collapse (sd) `order'
	tempfile panel3
	save `panel3', replace
	restore
	
	foreach var in `estimators'{
	  replace `var' = abs(`var')
	}

	preserve
	collapse (mean) `order', by(design)
	tempfile panel1
	save `panel1', replace
	restore
	
	preserve
	collapse (mean) `order'
	tempfile panel2
	save `panel2', replace
	restore
	
    preserve
    drop design curve __*
    xpose, clear varname
    forvalues i = 1(1)30{
      egen r`i'= rank(v`i')
     }
    drop v*
    xpose, clear
   	collapse (mean) `estimators'
	tempfile panel4
	save `panel4', replace
	restore
	if `k'<=4{
		use `panel1', clear
		gen stat = "Average |Bias x 1000| by Design"
		append using `panel2'
		replace stat = "Average |Bias x 1000|" if stat==""
		append using `panel3'
		replace stat = "S.D. of Bias x 1000" if stat==""
		append using `panel4'
		replace stat = "Average Rank of |Bias x 1000|" if stat==""
		order stat
        destring design, replace	
		replace stat = "" if design>1 & design<.
		tempfile bias_`k'
		save `bias_`k'', replace
    }
	
	if `k'>4{
		use `panel1', clear
		gen stat = "Average (Var x N) by Design"
		append using `panel2'
		replace stat = "Average (Var x N)" if stat==""
		append using `panel3'
		replace stat = "S.D. of (Var x N)" if stat==""
		append using `panel4'
		replace stat = "Average Rank of (Var x N)" if stat==""
		order stat
	    destring design, replace	
   	    replace stat = "" if design>1 & design<.
		tempfile var_`k'
		save `var_`k'', replace
    }
}	
	   use `bias_1',clear
	   append using `var_5'
       export excel "$dir/tables.xlsx", sheetreplace sheet("T1") firstrow(variables)
	
	   use `bias_2',clear
	   append using `var_6'
       export excel "$dir/tables.xlsx", sheetreplace sheet("T1-est") firstrow(variables)
	
	   use `bias_3',clear
	   append using `var_7'
       export excel "$dir/tables.xlsx", sheetreplace sheet("T1-001") firstrow(variables)

   	   use `bias_4',clear
	   append using `var_8'
       export excel "$dir/tables.xlsx", sheetreplace sheet("T1-est001") firstrow(variables)

// Table 2: NSW
	use "$dir/A2.dta", replace
	drop if regexm(col1,"bcm2")==1
	drop if regexm(col1,"bcm3")==1
	drop if regexm(col1,"nn2")==1
	drop if regexm(col1,"nn3")==1
	export excel "$dir/tables.xlsx", sheetreplace sheet("T2") firstrow(variables)
*/
// Table 3: Misspecified models
    local estimators = "nn1_X nn4_X bcm4_X nn1_pX nn4_pX bcm4_pX llr ipw1 ipw2 gpe"
    use "$dir/SIMSmisspec/simmisspec.dta", clear
	foreach var in `estimators'{
	  egen mean = mean(`var'), by(wiggle pscore_model biasadj_model)
	  gen var_`var'= 400*(`var'-mean)^2
	  gen bias_`var'= (`var'-truth)*1000
	  drop mean
	 }
	drop truth
	collapse (mean) bias_* var_* , by(wiggle pscore_model biasadj_model)
	gen wiggle1 = string(wiggle,"%9.1f")
	drop wiggle 
	tempfile mainfile
	save `mainfile', replace
    foreach stat in bias var{
      foreach w in 0.0 1.0 0.5 10.0 20.0{	
	    use `mainfile', clear
		keep if wiggle1=="`w'"
		local i = 1
		local namelist = "nn bcm"
		local postlist = "X pX"
		foreach name in `namelist'{
		 foreach post in `postlist'{
		  preserve
		  keep pscore_model biasadj_model `stat'_`name'4_`post'
		  gen estimator = "`name'_`post'"
		  ren `stat'_`name'4_`post' k4
		  tempfile f`i'
		  save `f`i'', replace
		  restore
		  local i = `i'+1
		 }
		}
		keep pscore_model biasadj_model *nn1* *llr *ipw1 *ipw2 *gpe
		local namelist = "nn1_X nn1_pX llr ipw1 ipw2 gpe"
		foreach name in `namelist'{
		  preserve
		  keep pscore_model biasadj_model `stat'_`name'
		  gen estimator = "`name'"
		  ren `stat'_`name' k4
		  tempfile f`i'
		  save `f`i'', replace
		  restore
		  local i = `i'+1
		 }
		local i = `i'-1
		di in red "`i'"
		use `f1', clear
		forvalues j = 2(1)`i'{
		 append using `f`j''
		}
		
	   duplicates tag k4 estimator, gen(tag)
	   replace pscore_model  = "-" if tag==15
	   replace biasadj_model = "-" if tag==15
	   duplicates drop
	   replace pscore_model = "-" if tag==3 & estimator=="bcm_X"
	   duplicates drop
	   drop tag
	   duplicates tag k4 estimator, gen(tag)
	   replace biasadj_model = "-" if tag==3
	   duplicates drop
	   drop tag
	   duplicates tag k4 estimator, gen(tag)
	   drop tag
	   order estimator
	   if "`w'"=="0.0" local wname="linear"
	   if "`w'"=="1.0" local wname="nonlinear1"
	   if "`w'"=="0.5" local wname="nonlinear2"
	   if "`w'"=="10.0"  local wname="nonlinear3"
	   if "`w'"=="20.0"  local wname="nonlinear4"
	   ren k4 `stat'_k4_`wname'
       tempfile file`wname'`stat'
	   save `file`wname'`stat'', replace
	}
	}
	use `filelinearbias', clear
	merge 1:1 estimator pscore_model biasadj_model using `filenonlinear1bias'
	drop _m
	merge 1:1 estimator pscore_model biasadj_model using `filenonlinear2bias'
    drop _m
	merge 1:1 estimator pscore_model biasadj_model using `filenonlinear3bias'
    drop _m
	merge 1:1 estimator pscore_model biasadj_model using `filenonlinear4bias'
    drop _m
	merge 1:1 estimator pscore_model biasadj_model using `filelinearvar'
	drop _m
	merge 1:1 estimator pscore_model biasadj_model using `filenonlinear1var'
	drop _m
	merge 1:1 estimator pscore_model biasadj_model using `filenonlinear2var'
    drop _m
	merge 1:1 estimator pscore_model biasadj_model using `filenonlinear3var'
    drop _m
	merge 1:1 estimator pscore_model biasadj_model using `filenonlinear4var'
    drop _m
	
    local estimators = "nn1_X nn_X bcm_X nn1_pX nn_pX bcm_pX llr ipw1 ipw2 gpe"
	gen order1 = .
	local i = 1
	foreach est in `estimators'{
	   replace order1 = `i' if estimator=="`est'"
	   local i = `i'+1
	}
	
    gen order2 = 1 if pscore_model =="-"
	replace order2 = 2 if pscore_model =="True Index"
	replace order2 = 3 if pscore_model =="Linear"
	replace order2 = 4 if pscore_model =="Interactions"
	replace order2 = 5 if pscore_model =="Linear+Interactions"

    gen order3 = 1 if biasadj_model =="-"
	replace order3 = 2 if biasadj_model =="True Index"
	replace order3 = 3 if biasadj_model =="Linear"
	replace order3 = 4 if biasadj_model =="Interactions"
	replace order3 = 5 if biasadj_model =="Linear+Interactions"

	
	sort order1 order2 order3
	keep estimator pscore_model biasadj_model bias_k4_linear var_k4_linear bias_k4_nonlinear1 var_k4_nonlinear1 bias_k4_nonlinear2 var_k4_nonlinear2 bias_k4_nonlinear3 var_k4_nonlinear3 bias_k4_nonlinear4 var_k4_nonlinear4 
    gen __S1__ = .
	gen __S2__ = .
	gen __S3__ = .
	gen __S4__ = .
    order estimator pscore_model biasadj_model bias_k4_linear var_k4_linear __S1__ bias_k4_nonlinear1 var_k4_nonlinear1 __S2__ bias_k4_nonlinear2 var_k4_nonlinear2 __S3__ bias_k4_nonlinear3 var_k4_nonlinear3 __S4__ bias_k4_nonlinear4 var_k4_nonlinear4
    export excel "$dir/tables.xlsx", sheetreplace sheet("T3") firstrow(variables)


//clean-up
forvalues k = 1(1)8{
 	erase "$dir/A1`k'.dta"
}
	erase "$dir/A2.dta"
	erase "$dir/A3a.dta"
	erase "$dir/A3b.dta"


